package Student.Kit;

import java.sql.*;
import java.util.Properties;

public class DBUtil {
    //    private static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";
//    private static final String JDBC_URL = "jdbc:mysql://localhost:3306/wanghaibo?useSSL=false&characterEncoding=UTF-8&serverTimezone=GMT%2b8";
//    private static final String SQLUSER_NAME = "root";
//    private static final String SQLUSER_PASSWORD = "123456";
    private static String JDBC_DRIVER;
    private static String JDBC_URL;
    private static String SQLUSER_NAME;
    private static String SQLUSER_PASSWORD;

    private DBUtil() {
    }

    //建立连接
    static {
        try {
            Properties pro = new Properties();
            pro.load(DBUtil.class.getResourceAsStream("/Day2/peizhi.properties"));
            JDBC_DRIVER = pro.getProperty("JDBC_DRIVER");
            JDBC_URL = pro.getProperty("JDBC_URL");
            SQLUSER_NAME = pro.getProperty("SQLUSER_NAME");
            SQLUSER_PASSWORD = pro.getProperty("SQLUSER_PASSWORD");
            Class.forName(JDBC_DRIVER);
        } catch (Exception e) {
            System.out.println("无法找到驱动类！");
            e.printStackTrace();
            System.exit(1);
        }
    }

    public static Connection getConnection() {
        Connection con = null;
        try {
            con = DriverManager.getConnection(JDBC_URL, SQLUSER_NAME, SQLUSER_PASSWORD);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return con;
    }

    public static void close(ResultSet res, Statement sta, Connection con) {
        try {
            if (res != null)
                res.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        try {
            if (sta != null)
                sta.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        try {
            if (con != null)
                con.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    public static void close(Connection con) {
        try {
            if (con != null)
                con.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    public static void close(Statement sta) {
        try {
            if (sta != null)
                sta.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    public static void close(ResultSet res) {
        try {
            if (res != null)
                res.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    public static int executeUpdate(String sql, Object... args) {
        Connection con = DBUtil.getConnection();
        PreparedStatement ps = null;
        try {
            ps = con.prepareStatement(sql);
            for (int i = 0; i < args.length; i++) {
                ps.setObject(i + 1, args[i]);
            }
            return ps.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DBUtil.close(null, ps, con);
        }
        return 0;
    }
}
